#coding=utf-8
from datetime import datetime,timedelta
from app.utils.util import  genPhone,genName
from app import db,app
from app.models import DataTask,DataDetail
import env_config
import random
from app.utils.account import register_auth_server,white,bindCard,login_auth_server
from app.utils.member import member
def gen_data():
    with app.app_context():
        list = db.session.query(DataTask).filter_by(status=0).order_by(DataTask.create_time.asc()).limit(1).first()
        if list is None:
            print('暂无待处理任务')
            return
        list.status = 1
        db.session.commit()
        gen_data_deal(list)
        list.status = 2
        db.session.commit()

def gen_data_deal(task):
    for n in range(1,1+task.data_num):
        data =DataDetail()
        data.taskid = task.id
        data.env = task.env
        data.phone = genPhone(task.phone_prefix,str(n))
        data.name = genName()
        data.idno=genIdno(task.idcard)
        data.bankno=genCardno(task.bankcard)
        data.bankname=task.bankcard
        data.is_bindcard = task.is_bindcard
        data.whitelist_system = task.whitelist_system
        data.is_whitelist = task.is_whitelist
        data.is_member = task.is_member
        data.member_type = task.member_type
        data.member_system = task.member_system
        data.is_register = task.is_register
        flag = True
        if data.is_register and flag:
            flag = register_auth_server(data)
        if data.is_whitelist and flag:
            flag = white(data)
        if data.is_member and flag:
            flag =  member(data)
        if data.is_bindcard and flag:
            flag = bindCard(data)
        db.session.add(data)
        db.session.commit()
def genCardno(bankname):
    if bankname ==  '随机分配':
        banks = env_config.BANK_LIST.keys()
        bankname = banks[random.randint(0, len(banks) - 1)]
    bankprefix = env_config.BANK_LIST[bankname].get('prefix')
    banklength = env_config.BANK_LIST[bankname].get('len')
    card1 = bankprefix + "".join(random.choice("0123456789") for i in range(banklength-len(bankprefix)-1))  # 地区项
    j = 0
    checkno = 0
    length = len(card1)
    for i in range(0, length):
        temp = int(card1[length - 1 - i])
        if j % 2 == 0:
            temp = temp * 2
            temp = temp // 10 + temp % 10
        j = j + 1
        checkno = checkno + temp

    checkno = 0 if checkno % 10 == 0 else 10 - checkno % 10
    card1 = card1 + str(checkno)
    return card1


# 随机生成身份证号
def genIdno(idcard):
    if idcard =='随机':
        idcard = ['成年','小于18岁'][random.randint(0,1)]
    if idcard == '成年':
        birthday = (datetime.now() - timedelta(days=random.randint(6588, 16060))).strftime("%Y%m%d")
    else:
        birthday = (datetime.now() - timedelta(days=random.randint(0, 6480))).strftime("%Y%m%d")
    id = env_config.DISTRICTLIST[random.randint(0, len(env_config.DISTRICTLIST) - 1)]  # 地区项
    id = id + "".join(random.choice("0123456789") for i in range(4)) +  birthday # 年份项
    id = id + str(random.randint(100, 999))  # ，顺序号简单处理
    i = 0
    count = 0
    weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]  # 权重项
    checkcode = {'0': '1', '1': '0', '2': 'X', '3': '9', '4': '8', '5': '7', '6': '6', '7': '5', '8': '5',
                 '9': '3',
                 '10': '2'}  # 校验码映射
    for i in range(0, len(id)):
        count = count + int(id[i]) * weight[i]
    id = id + checkcode[str(count % 11)]  # 算出校验码
    return id

if __name__=='__main__':
    data = DataDetail()
    data.is_register = True
    data.is_whitelist= True
    data.is_member= True
    data.is_bindcard= True
    data.member_type='黄金年卡'
    data.member_system='消费贷'
    data.whitelist_system='循环贷'
    data.phone='17611100010'
    data.password='111aaa'
    data.name='乌十十'
    data.idno='423713197609305959'
    data.bankname='光大银行'
    data.bankno='6214927193537951'
    #data.kissoid='ff808081658de0780165c16cbcea006a'
    data.env='UAT'
    bindCard(data)
    flag = True
    flag = False
    if data.is_register and flag:
        flag = register_auth_server(data)
    if data.is_whitelist and flag:
        flag = white(data)
    if data.is_member and flag:
        flag = member(data)
    if data.is_bindcard and flag:
        flag = bindCard(data)